const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");

var reqUrl = "https://nodejs.cn/";

var obj = url.parse(reqUrl);//将url字符串转换为对象结构

var options = {
    hostname:"nodejs.cn",
    port:80,
    path:"/",
    method:"GET"
}

var request = http.request(options,(res) => {//得到的res是一个流
    var str = " ";
    res.on("data",(chunk) => {
        str += chunk;
    })
    res.on("end",() => {
        // console.log(str);
        var filePath = path.join(__dirname,"/index.html");
        fs.writeFileSync(filePath,str);
    })
})

//http.request方法一定要调用end方法
request.end();